package cn.zzp.order.service.impl;

import cn.zzp.basic.service.impl.BaseServiceImpl;
import cn.zzp.basic.util.PageList;
import cn.zzp.order.domain.OrderPetAcquisition;
import cn.zzp.order.dto.OrderDto;
import cn.zzp.order.enums.OrderEnum;
import cn.zzp.order.mapper.OrderPetAcquisitionMapper;
import cn.zzp.order.query.OrderPetAcquisitionQuery;
import cn.zzp.order.service.OrderPetAcquisitionService;
import cn.zzp.org.domain.Employee;
import cn.zzp.org.domain.Shop;
import cn.zzp.org.mapper.EmployeeMapper;
import cn.zzp.org.mapper.ShopMapper;
import cn.zzp.pet.domain.Pet;
import cn.zzp.pet.domain.SearchMasterMsg;
import cn.zzp.pet.enums.SearchMasterMsgEnum;
import cn.zzp.pet.mapper.PetMapper;
import cn.zzp.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @ClassName OrderPetAcquisitionServiceImpl
 * @Description
 * @Author 祝志鹏
 * @Date 2022/8/11 11:59
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements OrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;

    /**
     * 分页查询
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, Long id) {
        // 查出当前用户信息
        Employee employee = employeeMapper.queryByLoginInfoId(id);
       // 判断员工类型
        if (employee.getShop_id() != null) { // 店铺工作人员
            Shop shop = shopMapper.queryById(employee.getShop_id());
            query.setShop_id(shop.getId());
            // 再判断是店长还是员工
            if (shop.getAdmin_id() != employee.getId()) { // 员工
                query.setEmp_id(employee.getId());
            }
        }

        return super.pageQuery(query);
    }

    /**
     * 订单确认
     * @param orderDto
     */
    @Transactional
    @Override
    public void confirm(OrderDto orderDto) {
        Long id = orderDto.getId();
        BigDecimal costprice = orderDto.getMoney();
        Integer payType = orderDto.getPayType();

        OrderPetAcquisition order = orderPetAcquisitionMapper.queryById(id);
        // 修改订单状态
        order.setState(OrderEnum.CONFIRM.getState()); //  完成
        order.setPrice(costprice);
        order.setPaytype(payType);
        orderPetAcquisitionMapper.update(order);

        // 修改寻主消息
        SearchMasterMsg smm = searchMasterMsgMapper.queryById(order.getSearch_master_msg_id());
        smm.setState(SearchMasterMsgEnum.PROCESSED.getState());
        smm.setNote(SearchMasterMsgEnum.PROCESSED.getDes());
        searchMasterMsgMapper.update(smm);

        // 修改宠物信息
        Pet pet = petMapper.queryById(order.getPet_id());
        pet.setCostprice(costprice);

        petMapper.update(pet);
    }

    /**
     * 取消订单
     * @param id
     */
    @Transactional
    @Override
    public void cancel(Long id) {

        // 修改订单状态
        OrderPetAcquisition order = orderPetAcquisitionMapper.queryById(id);
        order.setState(OrderEnum.CANCEL.getState());
        orderPetAcquisitionMapper.update(order);

        // 修改消息发布
        SearchMasterMsg smm = searchMasterMsgMapper.queryById(order.getSearch_master_msg_id());
        smm.setShop_id(null);
        smm.setState(SearchMasterMsgEnum.REJECT.getState());
        smm.setNote(SearchMasterMsgEnum.REJECT.getDes());
        searchMasterMsgMapper.update(smm);
        // 删除宠物
        Pet pet = petMapper.queryById(order.getPet_id());
        petMapper.delete(pet.getId());
    }
}
